package edu.ccut.saturn.workflow.startWorkFlowModule.code;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import edu.ccut.saturn.component.ComponentFactory;
import edu.ccut.saturn.component.ComponentManager;
import edu.ccut.saturn.component.IComponent;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.operation.Operation;
import edu.ccut.saturn.workflow.common.Constants;
import edu.ccut.saturn.workflow.common.WorkFlowTools;
import edu.ccut.saturn.workflow.model.ActivityInfo;
import edu.ccut.saturn.workflow.model.SubInfo;

public class ActivateSubActivity implements IActivateActivity {
	@SuppressWarnings("unchecked")
	public void activate(IParameterContext parameterContext) throws Exception{
		// 获取参数
		SaturnData workFlowInstance = (SaturnData) parameterContext
				.getParameterValue(0); 
		// previousActivityInstance 启动时：开始活动节点；完成时：完成活动实例
		SaturnData previousActivityInstance = (SaturnData) parameterContext
				.getParameterValue(3);
		Map<String, String> customParamMap = (Map<String, String>) parameterContext
				.getParameterValue(4);
		// 当前活动定义对象
		ActivityInfo currentActivityInfo = (ActivityInfo) parameterContext
				.getParameterValue(5);
		
		Connection conn = parameterContext.getConnection();
		// 获得当前活动数据项并附值
		SaturnData currentActivityInstance = WorkFlowTools.getActivityInstance(workFlowInstance, currentActivityInfo, conn);
		// 核心操作＝＝＝＝插入子流程活动实例
		Operation.dataBase().insert(currentActivityInstance,
				parameterContext.getConnection());
		// 核心操作＝＝＝＝插入流转实例
		SaturnData transitionInstance = WorkFlowTools.getTransitionInstance(
				conn, previousActivityInstance, currentActivityInstance);
		Operation.dataBase().insert(transitionInstance, conn);
		// 启动子流程
		SubInfo subProcessActivity=(SubInfo)currentActivityInfo;
		//*****为启动子流程设置参数*****
		HashMap<String,String> workFlowParamMap = new HashMap<String, String>();		
		workFlowParamMap.put("process_xml_key", subProcessActivity.getSubProcess());
		//查询有效流程定义
		/*String queryProcessDefine="select * from wf_process_define where process_xml_key = '"+subProcessActivity.getSubProcess()+"'"
							 +" and state <> 0";
		SaturnData processDefineInst=Operation.dataBase().queryBySQL("http://edu.ccut.saturn.workflow/persistenceModule/data::WF_PROCESS_DEFINE", queryProcessDefine, conn).get(0);*/
		HashMap<String,Object> paramMapForGetProcessDefineInst = new HashMap<String,Object>();
		paramMapForGetProcessDefineInst.put("process_xml_key", subProcessActivity.getSubProcess());
		SaturnData processDefineInst=Operation.dataBase().queryBySaturnSql(Constants.SATURNDATA_WF_PROCESS_DEFINE, 
				"GET_AVAILABILITY_DEFINE", paramMapForGetProcessDefineInst, conn).get(0);
		//设置流程版本
		workFlowParamMap.put("version", processDefineInst.get("version")+"");
		//设置创建者，同父流程创建者
		workFlowParamMap.put("creator", workFlowInstance.getString("creator"));
		workFlowParamMap.put("creator_name", workFlowInstance.getString("creator_name"));
		//TODO business_type,business_id
		//设置父活动ID和父流程ID
		workFlowParamMap.put("parent_activity_id", subProcessActivity.getId());
		workFlowParamMap.put("parent_process_instance_id", workFlowInstance.get("process_instance_id")+"");
		// 子流程的名称和描述
		workFlowParamMap.put("name",processDefineInst.get("name")+"");
		workFlowParamMap.put("description", processDefineInst.get("description")+"");
		
		// 设置子流程的参数上下文
		IParameterContext subProcessParameterContext = ComponentFactory.createParameterContext();
		subProcessParameterContext.addParameter(workFlowParamMap, java.util.HashMap.class);
		subProcessParameterContext.addParameter(customParamMap, java.util.HashMap.class);
		// 执行启动子流程的操作组件
		IComponent startWfLogic = ComponentManager.getInstance().getComponent( "edu.ccut.saturn.workflow::/startWorkFlowModule/logic/StartWorkFlow.logic");
		startWfLogic.execute(subProcessParameterContext);
		//TODO connection should be closed
		subProcessParameterContext=null;
		
		// 查询出子流程的流程的流程实例
	/*	String querySubProcessInstSql = "select * from wf_process_instance where parent_process_instance_id="+workFlowInstance.get("process_instance_id")+""
				     +" and parent_activity_id = "+subProcessActivity.getId();	 
		SaturnData subProcessInst = Operation.dataBase().queryBySQL("http://edu.ccut.saturn.workflow/persistenceModule/data::WF_PROCESS_INSTANCE", querySubProcessInstSql, conn).get(0);*/
		
		HashMap<String,Object> paramMapForGetSubProcessInst = new HashMap<String,Object>();
		paramMapForGetSubProcessInst.put("parent_activity_id", subProcessActivity.getId());
		paramMapForGetSubProcessInst.put("parent_process_instance_id", workFlowInstance.get("process_instance_id")+"");
		
		SaturnData subProcessInst = Operation.dataBase().queryBySaturnSql(Constants.SATURNDATA_WF_PROCESS_INSTANCE, 
				"GET_SUBPROCESSINST_BY_PARENTPROCESSINSTID", paramMapForGetSubProcessInst, conn).get(0);
		// 更新子流程活动的sub_process_instance_id
		currentActivityInstance.setString("sub_process_instance_id", subProcessInst.get("process_instance_id")+"");
		Operation.dataBase().updateByPrimaryKey(currentActivityInstance,"",conn);
		
		// 判断子流程的调用方式
		// 如果是异步，则处理下一活动
		if(Constants.SUBPRORCESS_TRANSFERMODE_ASYNCHRONISM.equals(subProcessActivity.getTransferMode())){
			WorkFlowTools.checkToActivateActivity(parameterContext);
		}
		
	}
}
